<?php

namespace Common\Model;

use Think\Model;

class BbsReplyChatModel extends Model
{
    protected $_auto = array(
        array('addtime', 'time', 1, 'function'),
    );

    /**
     * 获取列表
     * @param array  $map
     * @param bool   $page
     * @param string $sort
     * @param int    $num
     * @return mixed
     */
    public function get_list($map = array(), $page = false, $sort = '', $num = 10)
    {
        $limit = $num ? $num : 10;

        if (empty($sort)) {
            $sort = 'id DESC';
        }
        if ($page) {
            $count = $this->where($map)->count();
            $Page = pager($count, $limit);
            $limit = $Page->firstRow . ',' . $Page->listRows;
            $page_str = $Page->fshow();
        }

        $list = $this->where($map)->order($sort)->limit($limit)->select();

        $list = array_map(array($this,'fill'), $list);

        $return['list'] = $list;
        $return['count'] = $page ? $count : count($list);
        $return['page'] = $page_str;

        return $return;
    }

    /**
     * 添加回帖聊天
     * @param $data
     * @param $rid
     * @param $send_uid
     * @param $to_uid
     */
    public function chat_add($data, $rid, $send_uid, $to_uid)
    {
        // 保存数据
        if (!$this->create($data)) {
            $ret['status'] = false;
            $ret['msg'] = $this->getError();
            return $ret;
        }
        $this->rid = $rid;
        $this->send_uid = $send_uid;
        $this->to_uid = $to_uid;
        $newid = $this->add();
        if (!$newid) {
            $ret['status'] = false;
            $ret['msg'] = '保存失败';
        } else {
            # 保存成功相关操作
            $ret['status'] = true;
            $ret['msg'] = '保存成功';
        }
        return $ret;
    }

    /**
     * 字段填充
     * @param $item
     * @return mixed
     */
    public function fill($item)
    {
        return $item;
    }
}